import {BasicProps, MethodsType} from './types/basicMap.ts';
import AMapLoader from '@amap/amap-jsapi-loader';
import {onUnmounted, ref} from 'vue';

export function useBasicMap(basicProps: BasicProps) {
    const {mapKey, mapPlugin, option, defaultOption, el} = basicProps as BasicProps;
    const wrapperRef = ref(null);
    let Amap = null
    let mapIsLoaded = ref(false);
    function register(mapInstance: any, uuid: string) {
        onUnmounted(() => {
            if(wrapperRef.value?.hasOwnProperty('destroy')){
                wrapperRef.value.destroy();
            }
            wrapperRef.value = null;
        });
        return new Promise((resolve) => {
            AMapLoader.load({
                key: mapKey, // 申请好的Web端开发者Key，首次调用 load 时必填
                version: '2.0', // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
                plugins: mapPlugin, // 需要使用的的插件列表，如比例尺'AMap.Scale'等
            }).then((res) => {
                const {zoom, center, mapStyle} = {...defaultOption, ...option};
                //基本地图加载
                Amap = res
                mapIsLoaded.value = true;
                wrapperRef.value = new Amap.Map(el, {
                    resizeEnable: true,
                    center: center,
                    zoom: zoom,
                    mapStyle: 'amap://styles/' + mapStyle,
                });
                // 异步同时加载多个插件
                new Amap.plugin(mapPlugin, () => {
                });
                let marker = new Amap.Marker({
                    position: [115.405467, 40.907761], // Marker的经纬度位置
                    title: '我的Marker', // Marker的标题
                });
                marker.on('click', (e) => {
                    alert('Marker被点击了！');
                    // 在这里可以获取Marker的信息，如经纬度、标题等
                });
                // 添加Marker到地图上
                wrapperRef.value.add(marker);
                resolve();
            });
        });
    }

    let amapLoadedPromise = new Promise((resolve, reject) => {
        // 假设你有一个方法来检查Amap是否已加载
        function checkAmapLoaded() {
            if (Amap) {
                resolve(Amap); // 地图已加载，解析Promise
            } else {
                // 稍后再试
                setTimeout(checkAmapLoaded, 200);
            }
        }

        checkAmapLoaded();
    });

    async function waitForAmap() {
        return await amapLoadedPromise;
    }

    const methods: MethodsType = {
        getMap: () => {
            return wrapperRef
        },
        getGeocoder: async () => {
            let res = new Amap.Geocoder({
                radius: 1000,
            })
            return new Promise((resolve, reject) => {
                let lng = new Amap.LngLat(108.939645, 34.343207)
                res.getAddress(lng, (status, result) => {
                    if (status === 'complete') {
                        return resolve(result)
                    } else {
                        return reject('根据地址查询位置失败')
                    }
                })
            })
        }
    };
    return [register, methods];
}
